.text
.section .rodata
.text
.globl main
.type main, @function
main:
    pushq %rbp
    movq %rsp, %rbp

    subq $16, %rsp
    leaq -10(%rbp), %rdi
    movl $10, %esi
    call s_gets

    leaq -10(%rbp), %rdi
    call puts

    movl $0, %eax
    movq %rbp, %rsp
    popq %rbp
    ret

.text
.globl s_gets
.type s_gets, @function
s_gets:
    pushq %rbp
    movq %rsp, %rbp

    // 得琢磨一下减多少
    // sizeof(st) + sizeof(n) + sizeof(ret_val) + sizeof(i) = 8 + 4 + 8 + 4 = 24
    subq $32, %rsp

    // 分配局部变量
    // st: -8(%rbp)
    // n: -12(%rbp)
    // ret_val: -20(%rbp)
    // i: -24(%rbp)
    movq %rdi, -8(%rbp) /* st */
    movl %esi, -12(%rbp) /* n */
    movl $0, -24(%rbp) /* i */
    // 这两句实际是没必要的
    movq -8(%rbp) /* st */, %rdi
    movl -12(%rbp) /* n */, %esi
    movq stdin(%rip), %rdx
    call fgets
    movq %rax, -20(%rbp) /* ret_val */

    cmpq $0, %rax
    je .EXIT
    jmp .WHILE_15_COMPARE
.WHILE_15_ENTER:
    incl -24(%rbp) /* i */
.WHILE_15_COMPARE:
    movq -8(%rbp) /* st */, %rdi
    movl -24(%rbp) /* i */, %eax
    cltq
    movb (%rdi, %rax, 1), %al
    cmpb $10, %al
    je .WHILE_15_BREAK
    cmpb $0, %al
    je .WHILE_15_BREAK
    jmp .WHILE_15_ENTER
.WHILE_15_BREAK:
    movq -8(%rbp) /* st */, %rdi
    movl -24(%rbp) /* i */, %eax
    cltq
    movb (%rdi, %rax, 1), %al
    cmpb $10, %al
    jne .WHILE_20_COMPARE
    movl -24(%rbp) /* i */, %eax
    cltq
    movb $0, (%rdi, %rax, 1)
    jmp .EXIT

.WHILE_20_COMPARE:
    call getchar
    cmpb $10, %al
    jne .WHILE_20_COMPARE
.EXIT:
    movq -20(%rbp) /* ret_val */, %rax
    movq %rbp, %rsp
    popq %rbp
    ret